﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Generico.Coneccion;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Text;
using System.Xml;
using System.IO;
using SelectPdf;

namespace RosarioFinanzas
{
    public partial class Formulario_web12 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = 0;

            try
            {

                var articulo = new WebRosarioFinanzas.Service1().GetArticuloRow(Page.Request.QueryString["id"].ToString());

                //Add Meta Tag
                HtmlMeta title = new HtmlMeta();
                title.HttpEquiv = "title";
                title.Name = "title";
                title.Content =  System.Web.HttpUtility.HtmlDecode(Regex.Replace(articulo["Titulo"].ToString(), "<.*?>", string.Empty).Replace("\n", "").Replace("\r", "").Replace("\t", ""));
                this.Page.Header.Controls.Add(title);

                //Add Description Meta Tag
                HtmlMeta description = new HtmlMeta();
                description.HttpEquiv = "description";
                description.Name = "description";
                description.Content =  System.Web.HttpUtility.HtmlDecode(Regex.Replace(articulo["Resumen"].ToString(), "<.*?>", string.Empty).Replace("\n", "").Replace("\r", "").Replace("\t", ""));
                this.Page.Header.Controls.Add(description);

                // Social Media Tags
                HtmlMeta tagOgTitle = new HtmlMeta();
                tagOgTitle.Attributes.Add("property", "og:title");
                tagOgTitle.Content =  System.Web.HttpUtility.HtmlDecode(Regex.Replace(articulo["Titulo"].ToString(), "<.*?>", string.Empty).Replace("\n", "").Replace("\r", "").Replace("\t", ""));
                Page.Header.Controls.Add(tagOgTitle);

                HtmlMeta tagOgDescription = new HtmlMeta();
                tagOgDescription.Attributes.Add("property", "og:description");
                tagOgDescription.Content =  System.Web.HttpUtility.HtmlDecode(Regex.Replace(articulo["Resumen"].ToString(), "<.*?>", string.Empty).Replace("\n", "").Replace("\r", "").Replace("\t", ""));
                Page.Header.Controls.Add(tagOgDescription);

                HtmlMeta tagOgImage = new HtmlMeta();
                tagOgImage.Attributes.Add("property", "og:image");
                tagOgImage.Content = "http://rosariofinanzas.com.ar/Imagenes/Cabecera_FB.png";
                Page.Header.Controls.Add(tagOgImage);

                HtmlMeta tagOgUrl = new HtmlMeta();
                tagOgUrl.Attributes.Add("property", "og:url");
                tagOgUrl.Content = "http://rosariofinanzas.com.ar/Articulos.aspx?id=" + Page.Request.QueryString["id"].ToString();
                Page.Header.Controls.Add(tagOgUrl);
            }             
            catch (Exception)
            {
                Response.Redirect("~/Secciones.aspx?Secciones=INFORMES&SubSeccion=Todas");
            }
        }


        protected void ConvertirHTMLtoPDF(object sender, EventArgs e)
        {
            try
            {
              
                // read parameters from the webpage
                string url = Request.Url.AbsoluteUri;

                PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), "A4", true);

                PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), "Portrait", true);

                int webPageWidth = 1024;
                try
                {
                    webPageWidth = Convert.ToInt32("1024");
                }
                catch { }

                int webPageHeight = 0;
                try
                {
                    webPageHeight = Convert.ToInt32("768");
                }
                catch { }

                // instantiate a html to pdf converter object
                HtmlToPdf converter = new HtmlToPdf();

                // set converter options
                converter.Options.PdfPageSize = pageSize;
                converter.Options.PdfPageOrientation = pdfOrientation;
                converter.Options.WebPageWidth = webPageWidth;
                converter.Options.WebPageHeight = webPageHeight;
                converter.Options.MarginTop = 10;
                converter.Options.MarginLeft = 10;
                converter.Options.MarginRight = 10;
                converter.Options.MarginBottom = 10;


                // create a new pdf document converting an url
                PdfDocument doc = converter.ConvertUrl(url);

                // save pdf document
                doc.Save(Response, false, "Articulo.pdf");

                // close pdf document
                doc.Close();

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }


        [WebMethod]
        public static string strFecha(string str)
        {

            string[] dayarray = { "Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado" };
            string[] montharray = { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" };
            var daym = DateTime.Today.Day.ToString();
            var year = DateTime.Today.Year.ToString();
            var day = Convert.ToInt32(DateTime.Today.DayOfWeek);
            var month = DateTime.Today.Month - 1;
            return dayarray[day] + ", " + daym + " de " + montharray[month] + " del " + year + " ";

        }
        [WebMethod(EnableSession = true)]
        public static string strComentar()
        {
            string str = string.Empty;
            if (HttpContext.Current.Session["IdUsuario"] != null)
            {
                str = "Comentar";

            }
            return str;
        }

        [WebMethod(EnableSession = true)]
        public static string Comentario(string id)
        {
            var cn = new ClsConeccion();
            cn.Abrir();
            cn.AgregarParametros("idArticulo", Convert.ToInt32(id));
            var rd = cn.EjecutarSpDataReader("sp_GetComentarioArticulo");
            var dt = new DataTable();
            dt.Load(rd);
            string str = string.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                str += "<div class=\"Comentario\"><div class=\"Usuario\">Usuario:" + dr["Usuario"].ToString() + "</div>";
                str += "<div class=\"Comenta\">Comentario:" + dr["Comentario"].ToString() + "</div></div>";
            }
            cn.Cerrar();

            return str;
            
        }


    }
}
